# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

import datetime



import logging
_logger = logging.getLogger(__name__)

class CrEmpWarning(models.Model):

    _name = "cr.emp.warning"
    _description = "预警"

    name = fields.Char(string='姓名',related='staff_id.name',store=True)
    card_id = fields.Char(string='工号',related='staff_id.card_id',store=True)
    ws_id = fields.Many2one('cr.emp.workshop', string='所属车间', related='staff_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team", string='班组', related='staff_id.team_id',store=True)
    post_id = fields.Many2one("cr.emp.post", string='岗位', related='staff_id.post_id',store=True)
    staff_id = fields.Many2one('cr.emp.staff',string='员工')

    time = fields.Datetime(string='记录时间')
    # score_id = fields.Many2one('cr.emp.score',string='积分')
    warning_detail_ids = fields.One2many('cr.emp.warning.item','warning_id',string='预警条目')

    year = fields.Char(string='年份',compute='_compute_time',store=True)
    month = fields.Char(string='月份',compute='_compute_time',store=True)

    @api.depends('time')
    def _compute_time(self):
        for i in self:
            i.year=i.time.year
            i.month=i.time.month
    # @api.depends('score_id')
    # def _compute_score(self):
    #     #lqy
    #     pass


class CrEmpWarningItem(models.Model):

    _name = "cr.emp.warning.item"
    _description = "预警条目"

    name = fields.Char(string='姓名',related='staff_id.name',store=True)
    card_id = fields.Char(string='工号',related='staff_id.card_id',store=True)
    ws_id = fields.Many2one('cr.emp.workshop', string='所属车间', related='staff_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team", string='班组', related='staff_id.team_id',store=True)
    post_id = fields.Many2one("cr.emp.post", string='岗位', related='staff_id.post_id',store=True)
    staff_id = fields.Many2one('cr.emp.staff',string='员工')

    warning_card_id = fields.Char(string='预警人工号')
    warning_staff_id = fields.Many2one('cr.emp.staff',string='员工')
    # warning_name = fields.Char(string='防控姓名',related='warning_staff_id.name',store=True)

    warning_id= fields.Many2one('cr.emp.warning',string='预警')
    time = fields.Date(string='日期')
    warning_fault = fields.Text(string='预警问题')
    measures = fields.Text(string='整改措施')
    year = fields.Char(string='年份',compute='_compute_time',store=True)
    month = fields.Char(string='月份',compute='_compute_time',store=True)

    @api.depends('time')
    def _compute_time(self):
        for i in self:
            i.year=i.time.year
            i.month=i.time.month

    @api.model
    def create(self, values):
        warning_item=super(CrEmpWarningItem, self).create(values)
        warning=self.env['cr.emp.warning'].search([('staff_id','=',warning_item.staff_id.id)])[0]
        warning_item.write({'warning_id':warning.id})
        warning.write({'time':warning_item.time})
        return warning_item
